/**
 * 
 */
package GUI;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;

import Entity.Event;
import Entity.Meeting;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.SqlDateModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JScrollPane;

/**
 * @author aviat
 *
 */
public class EditMeeting extends JPanel{

	
	private static final long serialVersionUID = 1L;
	private JButton btnBack;
	private JLabel iconlabel;
	private JLabel lblAdress;
	private JLabel lblDate;
	private JLabel lblTime;
	private JButton btnChange;
	private JTextField TimetextField;
	private JComboBox<String> AddresscomboBox;
	private JDatePickerImpl mdatePicker;
	private JTextField FormerDatetextField;
	private JTextField NewTimetextField;
	private JLabel label;
	private JLabel lblCustomerFirstName;
	private JComboBox<String> GroomNameComboBox;
	private JComboBox<String> BrideNameComboBox;
	private JLabel lblCustomerLastName;
	
	private ArrayList<Meeting> c_allData;
	private ArrayList<Meeting> c_filteredData;
	private Object syncObject;
	private boolean m_isChangeAllowed;
	private Integer cID;
	private JLabel lblEventDate;
	private JComboBox<String> eDateComboBox;
	private JButton btnRefresh;
	private JScrollPane scrollPane;

	/**
	 * Constructor
	 */
	public EditMeeting() {
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(EditMeeting.class.getResource("/resources/back22.png")));
		NewTimetextField = new JTextField("hh:mm:ss");
		btnChange = new JButton("Change");
		TimetextField = new JTextField();
		TimetextField.setEditable(false);
		FormerDatetextField = new JTextField();
		FormerDatetextField.setEditable(false);
		GroomNameComboBox = new JComboBox<String>();
		btnRefresh = new JButton("refresh");
		
		initialize();
	}

	private void initialize() {
		JLabel lblEditMeeting = new JLabel("Edit Meeting");
		lblEditMeeting.setForeground(new Color(0, 204, 255));
		lblEditMeeting.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblEditMeeting.setBounds(10, 11, 143, 25);
		add(lblEditMeeting);
		
		btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnBack.setBounds(10, 462, 101, 27);
		add(btnBack);
		
		ImageIcon LOGOImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resources/camera-background3.png"));
		ImageIcon LOGO = new ImageIcon (LOGOImageIcon.getImage().getScaledInstance(-1, -1, java.awt.Image.SCALE_SMOOTH));
		
		lblAdress = new JLabel("Address");
		lblAdress.setForeground(Color.WHITE);
		lblAdress.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblAdress.setBounds(130, 177, 99, 27);
		add(lblAdress);
		
		lblDate = new JLabel("New Date");
		lblDate.setForeground(Color.WHITE);
		lblDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblDate.setBounds(393, 256, 64, 25);
		add(lblDate);
		
		lblTime = new JLabel("Former Time");
		lblTime.setForeground(Color.WHITE);
		lblTime.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblTime.setBounds(128, 324, 89, 27);
		add(lblTime);
		
		
		btnChange.setIcon(new ImageIcon(EditMeeting.class.getResource("/resources/change.png")));
		btnChange.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnChange.setBounds(439, 396, 122, 27);
		add(btnChange);

		SqlDateModel model2 = new SqlDateModel();
		JDatePanelImpl datePanel2 = new JDatePanelImpl(model2);
		mdatePicker = new JDatePickerImpl(datePanel2);
		mdatePicker.setShowYearButtons(true);
		mdatePicker.getJFormattedTextField().setFont(new Font("Tahoma", Font.BOLD, 12));
		mdatePicker.setBounds(491, 256, 151, 27);
		add(mdatePicker);
		
		
		TimetextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		TimetextField.setBounds(227, 328, 110, 27);
		add(TimetextField);
		TimetextField.setColumns(10);
		
		JLabel lblFormerDate = new JLabel("Former Date");
		lblFormerDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFormerDate.setForeground(Color.WHITE);
		lblFormerDate.setBounds(128, 256, 89, 27);
		add(lblFormerDate);
		
		
		FormerDatetextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		FormerDatetextField.setBounds(227, 256, 110, 27);
		add(FormerDatetextField);
		FormerDatetextField.setColumns(10);
		
		JLabel lblNewTime = new JLabel("New Time");
		lblNewTime.setForeground(Color.WHITE);
		lblNewTime.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblNewTime.setBounds(393, 328, 64, 27);
		add(lblNewTime);
		
		
		NewTimetextField.setFont(new Font("Tahoma", Font.BOLD, 12));
		NewTimetextField.setBounds(491, 332, 110, 27);
		add(NewTimetextField);
		NewTimetextField.setColumns(10);
		
		label = new JLabel("");
		label.setBounds(47, 101, 46, 14);
		add(label);
		
		lblCustomerFirstName = new JLabel("Groom Name:");
		lblCustomerFirstName.setForeground(Color.WHITE);
		lblCustomerFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCustomerFirstName.setBounds(132, 114, 99, 27);
		add(lblCustomerFirstName);
		
		
		GroomNameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(GroomNameComboBox.getSelectedItem().toString(),"GROOM_NAME");
				}
			}
		});
		GroomNameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		GroomNameComboBox.setBounds(227, 114, 144, 27);
		add(GroomNameComboBox);
		
		BrideNameComboBox = new JComboBox<String>();
		BrideNameComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(BrideNameComboBox.getSelectedItem().toString(),"BRIDE_NAME");
				}
			}
		});
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(227, 177, 144, 27);
		add(scrollPane);
		AddresscomboBox = new JComboBox<String>();
		AddresscomboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Manager", "Worker", "Secretary"}));
		scrollPane.setViewportView(AddresscomboBox);
		AddresscomboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
						filterList(AddresscomboBox.getSelectedItem().toString(),"ADDRESS");
					
				}
			}
		});
		AddresscomboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		AutoCompletion.enable(this.AddresscomboBox);
		BrideNameComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		BrideNameComboBox.setBounds(490, 114, 110, 27);
		add(BrideNameComboBox);
		
		eDateComboBox = new JComboBox<String>();
		eDateComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed()){
						filterList(eDateComboBox.getSelectedItem().toString(),"EDATE");
						
					}
				}
			}
		});
		eDateComboBox.setFont(new Font("Tahoma", Font.BOLD, 12));
		eDateComboBox.setBounds(491, 177, 110, 27);
		add(eDateComboBox);
		lblCustomerLastName = new JLabel("Bride Name:");
		lblCustomerLastName.setForeground(Color.WHITE);
		lblCustomerLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCustomerLastName.setBounds(381, 114, 99, 27);
		add(lblCustomerLastName);
		
		lblEventDate = new JLabel("Event Date:");
		lblEventDate.setForeground(Color.WHITE);
		lblEventDate.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblEventDate.setBounds(381, 177, 99, 27);
		add(lblEventDate);
		
		
		btnRefresh.setIcon(new ImageIcon(EditMeeting.class.getResource("/resources/arrow-refresh-2-icon.png")));
		btnRefresh.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 12));
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				synchronized (syncObject) {
					if(isChangeAllowed())
					{
						FormerDatetextField.setText("");
						TimetextField.setText("");
						NewTimetextField.setText("hh:mm:ss");
						filterList(null,"ALL");
					}
				}
			}
		});
		btnRefresh.setBounds(281, 396, 122, 27);
		add(btnRefresh);
		
		iconlabel = new JLabel("");
		iconlabel.setIcon(LOGO);
		iconlabel.setBounds(0, 0, 786, 500);
		add(iconlabel);
		
		
	}

	/**
	 * @return the timetextField
	 */
	public JTextField getTimetextField() {
		return TimetextField;
	}

	/**
	 * @param timetextField the timetextField to set
	 */
	public void setTimetextField(JTextField timetextField) {
		TimetextField = timetextField;
	}

	/**
	 * @return the custIDcomboBox
	 */
	public JComboBox<String> getCustIDcomboBox() {
		return AddresscomboBox;
	}

	/**
	 * @param custIDcomboBox the custIDcomboBox to set
	 */
	public void setCustIDcomboBox(JComboBox<String> custIDcomboBox) {
		this.AddresscomboBox = custIDcomboBox;
	}
	/**
	 * @return the meeting date in SQL format
	 */
	public String getMeetingDate()
	{
		java.sql.Date selectedDate = (java.sql.Date)mdatePicker.getModel().getValue();
		if(selectedDate == null) return "";
		return selectedDate.toString();
	}

	/**
	 * @return the newTimetextField
	 */
	public JTextField getNewTimetextField() {
		return NewTimetextField;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}

	/**
	 * @return the btnChange
	 */
	public JButton getBtnChange() {
		return btnChange;
	}

	/**
	 * @return the formerDatetextField
	 */
	public JTextField getFormerDatetextField() {
		return FormerDatetextField;
	}

	/**
	 * @return the eDateComboBox
	 */
	public JComboBox<String> geteDateComboBox() {
		return eDateComboBox;
	}

	/**
	 * @return the firstnameComboBox
	 */
	public JComboBox<String> getFirstnameComboBox() {
		return GroomNameComboBox;
	}
	public void setAllCustomersMeeting(ArrayList<Meeting> list){
		c_allData = list;
		c_filteredData = new ArrayList<>();
		syncObject = new Object();
		filterList(null,"ALL");
		 
	}
	
	private void filterList(String selectedItem, String scope)
	{
		synchronized (syncObject) {
			c_filteredData.clear();
			
			switch(scope)
			{
			case "GROOM_NAME":
				for (Meeting m :c_allData) 
				{
					if(m.getgName().equals(selectedItem))
					{
						c_filteredData.add(m);
					}
				}
				break;
			case "BRIDE_NAME":
				for (Meeting m :c_allData) 
				{
					if(m.getbName().equals(selectedItem))
					{
						c_filteredData.add(m);
					}
				}
				break;
			case "ADDRESS":
				for (Meeting m :c_allData) 
				{
					if(m.getcAddress().equals(selectedItem))
					{
						c_filteredData.add(m);
					}
				}
				break;
			case "EDATE":
				for (Meeting m :c_allData) 
				{
					if(m.geteDate().equals(selectedItem))
					{
						c_filteredData.add(m);
					}
				}
				break;
			case "ALL":
				System.out.println("in all");
				c_filteredData.addAll(c_allData);
				break;
			}
		}
		m_isChangeAllowed = false;
		filterComboBox(GroomNameComboBox,"GROOM_NAME");
		filterComboBox(BrideNameComboBox,"BRIDE_NAME");
		filterComboBox(AddresscomboBox,"ADDRESS");
		filterComboBox(eDateComboBox,"EDATE");
		if(GroomNameComboBox.getItemCount()==1 && BrideNameComboBox.getItemCount()==1
				&& AddresscomboBox.getItemCount()==1 && eDateComboBox.getItemCount()==1)
		{
			setmeetingDetails();
		}
		m_isChangeAllowed = true;

	}
	/**
	* This method adding items to combobox
	* @param combo is the combobox to add item to
	* @param scope is the key code for adding the correct field 
	* 
	*/
	private void filterComboBox(JComboBox<String> combo, String scope)
	{
		combo.removeAllItems();
		synchronized (syncObject)
		{
			for(Meeting m: c_filteredData )
			{
				switch(scope)
				{
				case "GROOM_NAME":
					combo.addItem(m.getgName());
					break;
				case "BRIDE_NAME":
					combo.addItem(m.getbName());
					break;
				case "ADDRESS":
					combo.addItem(m.getcAddress());
					break;
				case "EDATE":
					combo.addItem(m.geteDate());
					break;
				}
				
			}
		}
	}
	/**
	 * This method add the customers details
	 */
	public void setmeetingDetails(){
		
		for(Meeting m : c_filteredData){
			
			if(AddresscomboBox.getSelectedItem().toString().equals(m.getcAddress()))
			{
				FormerDatetextField.setText(m.getmDate());
				FormerDatetextField.setEditable(false);
				TimetextField.setText(m.getmTime());
				TimetextField.setEditable(false);
				cID = m.getcID();
			}
			break;
		}
	}
	/**
	* @return the e_isChangeAllowed
	* 
	*/
	private boolean isChangeAllowed()
	{
		return m_isChangeAllowed;
	}

	/**
	 * @return the cID
	 */
	public Integer getcID() {
		return cID;
	}

	/**
	 * @return the addresscomboBox
	 */
	public JComboBox<String> getAddresscomboBox() {
		return AddresscomboBox;
	}

	/**
	 * @param addresscomboBox the addresscomboBox to set
	 */
	public void setAddresscomboBox(JComboBox<String> addresscomboBox) {
		AddresscomboBox = addresscomboBox;
	}

	/**
	 * @return the mdatePicker
	 */
	public JDatePickerImpl getMdatePicker() {
		return mdatePicker;
	}

	/**
	 * @param mdatePicker the mdatePicker to set
	 */
	public void setMdatePicker(JDatePickerImpl mdatePicker) {
		this.mdatePicker = mdatePicker;
	}

	/**
	 * @return the groomNameComboBox
	 */
	public JComboBox<String> getGroomNameComboBox() {
		return GroomNameComboBox;
	}

	/**
	 * @param groomNameComboBox the groomNameComboBox to set
	 */
	public void setGroomNameComboBox(JComboBox<String> groomNameComboBox) {
		GroomNameComboBox = groomNameComboBox;
	}

	/**
	 * @return the brideNameComboBox
	 */
	public JComboBox<String> getBrideNameComboBox() {
		return BrideNameComboBox;
	}

	/**
	 * @param brideNameComboBox the brideNameComboBox to set
	 */
	public void setBrideNameComboBox(JComboBox<String> brideNameComboBox) {
		BrideNameComboBox = brideNameComboBox;
	}

	/**
	 * @return the m_isChangeAllowed
	 */
	public boolean isM_isChangeAllowed() {
		return m_isChangeAllowed;
	}

	/**
	 * @param m_isChangeAllowed the m_isChangeAllowed to set
	 */
	public void setM_isChangeAllowed(boolean m_isChangeAllowed) {
		this.m_isChangeAllowed = m_isChangeAllowed;
	}

	/**
	 * @return the btnRefresh
	 */
	public JButton getBtnRefresh() {
		return btnRefresh;
	}

	/**
	 * @param btnRefresh the btnRefresh to set
	 */
	public void setBtnRefresh(JButton btnRefresh) {
		this.btnRefresh = btnRefresh;
	}

	/**
	 * @param btnBack the btnBack to set
	 */
	public void setBtnBack(JButton btnBack) {
		this.btnBack = btnBack;
	}

	/**
	 * @param btnChange the btnChange to set
	 */
	public void setBtnChange(JButton btnChange) {
		this.btnChange = btnChange;
	}

	/**
	 * @param formerDatetextField the formerDatetextField to set
	 */
	public void setFormerDatetextField(JTextField formerDatetextField) {
		FormerDatetextField = formerDatetextField;
	}

	/**
	 * @param newTimetextField the newTimetextField to set
	 */
	public void setNewTimetextField(JTextField newTimetextField) {
		NewTimetextField = newTimetextField;
	}

	/**
	 * @param eDateComboBox the eDateComboBox to set
	 */
	public void seteDateComboBox(JComboBox<String> eDateComboBox) {
		this.eDateComboBox = eDateComboBox;
	}
}
